//Jeff Brinkhus
//Created 12-04-2009

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ResultPanel extends JPanel{
	private JPanel main, left, right, rowq, row1, row2, row3, row4, row5;
	private String[] possibleAns;
	private JLabel	quesNumLabel, quesLabel, r1, r2, r3, r4, r5, p1, p2, p3, p4, p5;
	private double[] percentage;
	private String[] oeResponses;
	private JTextArea textArea;
	private JRadioButton[] radioButtons;
	private JButton removeButton;
	private ButtonGroup buttonGroup;
	private JScrollPane scroll;
	private String usersResponse;
	private int type;
		
	public ResultPanel(Result r, int quesNum){
		// set possible answers, percentages, question number label, and question label
		quesNumLabel = new JLabel(quesNum+".");
		System.out.println("Before problem");
		System.out.println("Before problem" + r.getContent());
		quesLabel = new JLabel(r.getContent());
		type = r.getType();
		if(type == 2){
			possibleAns = r.getPossibleAns();
			percentage = r.getPercentages();
			main = new JPanel(new GridLayout(possibleAns.length+1,1));
		}else{
			oeResponses = r.getOEResponses();
			main = new JPanel(new GridLayout(oeResponses.length+1,1));
		}
		
		// Create main layout and flow layouts for each row
		this.setLayout(new BorderLayout());
		left = new JPanel(new FlowLayout());
		right = new JPanel(new FlowLayout());
		rowq = new JPanel(new FlowLayout());
		row1 = new JPanel(new FlowLayout());
		row2 = new JPanel(new FlowLayout());
		row3 = new JPanel(new FlowLayout());
		row4 = new JPanel(new FlowLayout());
		row5 = new JPanel(new FlowLayout());
		
		// add a question number and question label
		left.add(quesNumLabel);
		rowq.add(quesLabel);
		main.add(rowq);

		// checks if question is of multiple choice type
		if(r.getType() == 2){
			
			if(r.getPossibleAns().length >0){
				JLabel r1 = new JLabel(possibleAns[0]);
				JLabel p1 = new JLabel("\t   "+String.valueOf(percentage[0])+"%");
				row1.add(r1);
				row1.add(p1);
				main.add(row1);
			}
			
			if(r.getPossibleAns().length >1){
				JLabel r2 = new JLabel(possibleAns[1]);
				JLabel p2 = new JLabel("\t   "+String.valueOf(percentage[1])+"%");
				row2.add(r2);
				row2.add(p2);
				main.add(row2);
			}

			if(r.getPossibleAns().length >2){
				JLabel r3 = new JLabel(possibleAns[2]);
				JLabel p3 = new JLabel("\t   "+String.valueOf(percentage[2])+"%");
				row3.add(r3);
				row3.add(p3);
				main.add(row3);

			}

			if(r.getPossibleAns().length >3){
				JLabel r4 = new JLabel(possibleAns[3]);
				JLabel p4 = new JLabel("\t   "+String.valueOf(percentage[3])+"%");
				row4.add(r4);
				row4.add(p4);
				main.add(row4);
			}
			
			if(r.getPossibleAns().length >4){
				JLabel r5 = new JLabel(possibleAns[4]);
				JLabel p5 = new JLabel("\t   "+String.valueOf(percentage[4])+"%");
				row5.add(r5);
				row5.add(p5);
				main.add(row5);
			}
		}

		// question is not of multiple choice type and is open-ended
		else{ 
			for(int i=0; i<oeResponses.length; i++){
				textArea = new JTextArea(oeResponses[i],5,10);
				textArea.setEditable(false);
				scroll = new JScrollPane(textArea);
				scroll.setPreferredSize(new Dimension(250, 50));
				scroll.getVerticalScrollBar();
				scroll.getHorizontalScrollBar();
				main.add(scroll);
			}
		}

		this.add(left, BorderLayout.WEST);
		this.add(main, BorderLayout.CENTER);
		this.add(right, BorderLayout.EAST);
		this.setSize(1000, 500);
		this.setVisible(true);
	}
	
	public static void main(String[] args){
		JFrame f = new JFrame();
		JPanel main = new JPanel(new BorderLayout());
		JScrollPane scroll = new JScrollPane();
		JPanel jp = new JPanel(new GridLayout(4,1));
		
		scroll = new JScrollPane(main);
		scroll.getVerticalScrollBar();
		scroll.getHorizontalScrollBar();
		String[] pos1 = {"Aight","Okay","So-so","Not so good","Wish I never had to program again"};
		double[] d1 = new double[5];
		d1[0] = 10;
		d1[1] = 0;
		d1[2] = 10;
		d1[3] = 20;
		d1[4] = 60;
		Result res1 = new Result("How are you?", 1, pos1, d1);
		String[] rasdf2 = {"Remove","It","From","The","Required","Curriculum"};
		Result res2 = new Result("How can this course be improved?", 2, rasdf2);
		
		
		ResultPanel rp1 = new ResultPanel(res1, 1);
		ResultPanel rp2 = new ResultPanel(res2, 2);
		jp.add(rp1);
		jp.add(rp2);

		main.add(jp, BorderLayout.CENTER);
		f.add(scroll);
		f.setSize(1000, 500);
		f.setVisible(true);
	}
}